﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Top.Api;
using WangMiBusiness.BusinessConstant;
using Top.Api.Request;
using Top.Api.Response;
using WangMiEntity.EmployeeService;
using Wintellect.PowerCollections;
using WangMiEntity.GridViewModel;
using WangMiEntity.Order;
using WangMiBusiness.GridViewModel;
using WangMiBusiness.ExtendClass;

namespace WangMiBusiness.EmployeeService
{
    public class XiaoShoueBusiness
    {
        /// <summary>
        /// 按日期排列
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public List<XiaoShoueDateEntity> GetXiaoShoueByDate(DateTime startTime, DateTime endTime, string session, bool isContainALLDate, string sellerNick)
        {
            List<XiaoShoueDateEntity> xiaoShoueDateEntityList = new List<XiaoShoueDateEntity>();
            var gridViewModel = new GridViewModelBusiness().GetBaseGridViewModel(startTime, endTime, session);
            var orderPriceViewModel = GridViewHelper.GetOrderPrice(gridViewModel, sellerNick);

            orderPriceViewModel.ForEach(item =>
            {
                XiaoShoueDateEntity xiaoShoueDateEntity = new XiaoShoueDateEntity();
                xiaoShoueDateEntity.OrderDate = item.OrderDate;

                //总金额
                xiaoShoueDateEntity.TotalPrice = GetTotalOrderPrice(item);
                int totalCount = GetTotalOrderCount(item);
                xiaoShoueDateEntity.TotalCustomerPrice = totalCount == 0 ? 0 : (xiaoShoueDateEntity.TotalPrice / totalCount).RoundNumber();

                //未付款金额
                xiaoShoueDateEntity.UnPaidPrice = item.WaitBuyerPayPrice.RoundNumber();
                xiaoShoueDateEntity.UnpaidCustomerPrice = item.WaitBuyerPayTradeCount == 0 ? 0 : (xiaoShoueDateEntity.UnPaidPrice / item.WaitBuyerPayTradeCount).RoundNumber();

                //静默已付款金额
                xiaoShoueDateEntity.SilentPaidPrice = GetPaidPrice(item);
                int silentCount = GetPaidCount(item);
                xiaoShoueDateEntity.SilentPaidCustomerPrice = silentCount == 0 ? 0 : (xiaoShoueDateEntity.TotalPrice / silentCount).RoundNumber();

                //团队已付款金额
                xiaoShoueDateEntity.TeamPaidPrice = GetPaidPrice(item);
                xiaoShoueDateEntity.TeamPaidCustomerPrice = silentCount == 0 ? 0 : (xiaoShoueDateEntity.TotalPrice / silentCount).RoundNumber();

                //退款中金额
                xiaoShoueDateEntity.RefundingPrice = item.RefundingPrice.RoundNumber();
                xiaoShoueDateEntity.RefundingCustomerPrice = item.RefundingTradeCount == 0 ? 0 : (xiaoShoueDateEntity.RefundingPrice / item.RefundingTradeCount).RoundNumber();

                //已退款金额
                xiaoShoueDateEntity.RefundedPrice = item.RefundPrice.RoundNumber();
                xiaoShoueDateEntity.RefundCustomerPrice = item.RefundTradeCount == 0 ? 0 : (xiaoShoueDateEntity.RefundedPrice / item.RefundTradeCount).RoundNumber();

                xiaoShoueDateEntityList.Add(xiaoShoueDateEntity);
            });

            if (isContainALLDate)
            {
                xiaoShoueDateEntityList = GridViewHelper.GetAllDate(startTime, endTime, xiaoShoueDateEntityList);
            }

            return xiaoShoueDateEntityList;
        }

        /// <summary>
        /// 获取总订单金额
        /// </summary>
        /// <param name="orderPrice"></param>
        /// <returns></returns>
        public double GetTotalOrderPrice(OrderPrice orderPrice)
        {
            double totalOrderPaidPrice = 0;
            totalOrderPaidPrice += orderPrice.TradeNoCreatePayPrice;
            totalOrderPaidPrice += orderPrice.TradeClosedByTaoBaoPrice;
            totalOrderPaidPrice += orderPrice.WaitBuyerPayPrice;
            totalOrderPaidPrice += orderPrice.WaitSellerSendGoodsPrice;
            totalOrderPaidPrice += orderPrice.SellerConsignedPartPrice;
            totalOrderPaidPrice += orderPrice.WaitBuyerConfirmGoodsPrice;
            totalOrderPaidPrice += orderPrice.TradeBuyerSignedPrice;
            totalOrderPaidPrice += orderPrice.TradeFinishedPrice;
            totalOrderPaidPrice += orderPrice.TradeClosedByTaoBaoPrice;

            return totalOrderPaidPrice.RoundNumber();
        }

        /// <summary>
        /// 获取总订单数
        /// </summary>
        /// <param name="orderPrice"></param>
        /// <returns></returns>
        public int GetTotalOrderCount(OrderPrice orderPrice)
        {
            int totalOrderPaidCount = 0;
            totalOrderPaidCount += orderPrice.TradeNoCreatePayTradeCount;
            totalOrderPaidCount += orderPrice.TradeClosedByTaoBaoTradeCount;
            totalOrderPaidCount += orderPrice.WaitBuyerPayTradeCount;
            totalOrderPaidCount += orderPrice.WaitSellerSendGoodsTradeCount;
            totalOrderPaidCount += orderPrice.SellerConsignedPartTradeCount;
            totalOrderPaidCount += orderPrice.WaitBuyerConfirmGoodsTradeCount;
            totalOrderPaidCount += orderPrice.TradeBuyerSignedTradeCount;
            totalOrderPaidCount += orderPrice.TradeFinishedTradeCount;
            totalOrderPaidCount += orderPrice.TradeClosedByTaoBaoTradeCount;

            return totalOrderPaidCount;
        }

        /// <summary>
        /// 获取已付款金额
        /// </summary>
        /// <param name="orderPrice"></param>
        /// <returns></returns>
        public double GetPaidPrice(OrderPrice orderPrice)
        {
            double paidPrice = 0;
            paidPrice += orderPrice.WaitSellerSendGoodsPrice;
            paidPrice += orderPrice.SellerConsignedPartPrice;
            paidPrice += orderPrice.WaitBuyerConfirmGoodsPrice;
            paidPrice += orderPrice.TradeBuyerSignedPrice;
            paidPrice += orderPrice.TradeFinishedPrice;

            return paidPrice.RoundNumber();
        }

        /// <summary>
        /// 获取已付款数
        /// </summary>
        /// <param name="orderPrice"></param>
        /// <returns></returns>
        public int GetPaidCount(OrderPrice orderPrice)
        {
            int paidCount = 0;
            paidCount += orderPrice.WaitSellerSendGoodsTradeCount;
            paidCount += orderPrice.SellerConsignedPartTradeCount;
            paidCount += orderPrice.WaitBuyerConfirmGoodsTradeCount;
            paidCount += orderPrice.TradeBuyerSignedTradeCount;
            paidCount += orderPrice.TradeFinishedTradeCount;

            return paidCount;
        }
    }
}
